//
//  ServeViewController.m
//  智慧银行
//
//  Created by rimi on 15/9/28.
//  Copyright (c) 2015年 weilai. All rights reserved.
//

#import "ServeViewController.h"
#import "FinancialController.h"
#import "FundController.h"
#import "InsuranceController.h"
#import "PreciousMetalsController.h"
#import "CPDock.h"



@interface ServeViewController () <CPDockDelegate>
{
    CPDock *_dock;
    
    UIView *_contentView;
    NSArray *_arrController;
    UIViewController *_newController;
    UIViewController *_currentVC;
}


@end

@implementation ServeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = BACK_COLOR;
    _arrController = @[@"FinancialController",@"FundController",@"InsuranceController",@"PreciousMetalsController"];
    // 0.添加logo
    self.headView.image = [UIImage imageNamed:@"理财服务-logo.png"];
    
    // 1.添加Dock
    [self addDock];
    
    // 2.添加内容View
    [self addContentView];
    
    // 3.默认选中理财控制器
    [self dock:nil tabChangeFrom:0 to:0];

}


#pragma mark 添加Dock
- (void)addDock
{
    CPDock *dock = [[CPDock alloc] init];
    dock.frame = CGRectMake(180, 20, 480, 44);
    dock.delegate = self;
    [dock addOneTab:@"理财_25.png" selectedIcon:@"理财_选中.png" index:1];
    
    // 2.基金
    [dock addOneTab:@"基金_27.png" selectedIcon:@"基金_选中.png" index:2];
    
    // 3.保险
    [dock addOneTab:@"保险_29.png" selectedIcon:@"保险_选中.png" index:3];
    
    // 4.贵金属
    [dock addOneTab:@"贵金属_21.png" selectedIcon:@"贵金属_选中.png" index:4];
    [self.view addSubview:dock];
    _dock = dock;
}

#pragma mark 添加内容View
- (void)addContentView{
    _contentView = [[UIView alloc] init];
    CGFloat w = self.view.frame.size.width;
    CGFloat h = self.view.frame.size.height - 73;
    _contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    _contentView.frame = CGRectMake(0, 72, w, h);
    [self.view addSubview:_contentView];
}

#pragma mark 点击了Dock上的某个标签
- (void)dock:(CPDock *)dock tabChangeFrom:(NSInteger)from to:(NSInteger)to
{
    NSLog(@"%ld-%ld",from, to);
    // 1.先移除旧的
    UIViewController *oldVC = _currentVC;
    [oldVC.view removeFromSuperview];
    [oldVC removeFromParentViewController];
    [oldVC willMoveToParentViewController:nil];
    
    // 2.添加新的
    Class class2 = NSClassFromString(_arrController[to]);
    _newController = [[class2 alloc]init];
    _newController.view.frame = _contentView.bounds;
    [self addChildViewController:_newController];
    _currentVC = _newController;
    [_contentView addSubview:_newController.view];
}



@end


